//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SquareRootProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CSquareRootProcess::CSquareRootProcess (Double b, Double a, Double sigma, Microsoft::FSharp::Core::FSharpOption<Double>^ x0) : CStochasticProcess1D(CSquareRootProcess::typeid)
{
    try
    {
#ifdef HANDLE
        _phSquareRootProcess = NULL;
#endif
        QuantLib::Real _b = (QuantLib::Real)ValueHelper::Convert (b); //d
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a); //d
        QuantLib::Volatility _sigma = (QuantLib::Volatility)ValueHelper::Convert (sigma); //d
        QuantLib::Real _x0 = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (x0) ? (QuantLib::Real)ValueHelper::Convert (x0->Value) : 0.0); //4
        _ppSquareRootProcess = new boost::shared_ptr<QuantLib::SquareRootProcess> (new QuantLib::SquareRootProcess ( _b,  _a,  _sigma,  _x0 ));
        SetStochasticProcess1D (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppSquareRootProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Processes::CSquareRootProcess::CSquareRootProcess (boost::shared_ptr<QuantLib::SquareRootProcess>& childNative, Object^ owner) : CStochasticProcess1D(CSquareRootProcess::typeid)
{
#ifdef HANDLE
	_phSquareRootProcess = NULL;
#endif
	_ppSquareRootProcess = &childNative;
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppSquareRootProcess));
}
Cephei::QL::Processes::CSquareRootProcess::CSquareRootProcess (QuantLib::SquareRootProcess& childNative, Object^ owner) : CStochasticProcess1D(CSquareRootProcess::typeid)
{
#ifdef HANDLE
	_phSquareRootProcess = NULL;
#endif
	_ppSquareRootProcess = new boost::shared_ptr<QuantLib::SquareRootProcess> (&childNative);
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppSquareRootProcess));
    _SquareRootProcessOwner = owner;
    _StochasticProcess1DOwner = owner;
}

Cephei::QL::Processes::CSquareRootProcess::CSquareRootProcess (CSquareRootProcess^ copy) : CStochasticProcess1D(CSquareRootProcess::typeid)
{
#ifdef HANDLE
	_phSquareRootProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSquareRootProcess = new boost::shared_ptr<QuantLib::SquareRootProcess> (copy->GetShared());
        _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppSquareRootProcess));
    }
}
Cephei::QL::Processes::CSquareRootProcess::CSquareRootProcess (PLATFORM::Type^ t) : CStochasticProcess1D(CSquareRootProcess::typeid)
{
#ifdef HANDLE
	_phSquareRootProcess = NULL;
#endif
	if (!t->IsSubclassOf(CSquareRootProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CSquareRootProcess::CSquareRootProcess (QuantLib::Handle<QuantLib::SquareRootProcess>& childNative, Object^ owner)  : CStochasticProcess1D(CSquareRootProcess::typeid)
{
	_phSquareRootProcess = &childNative;
	_ppSquareRootProcess = &static_cast<boost::shared_ptr<QuantLib::SquareRootProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppSquareRootProcess));
    _SquareRootProcessOwner = owner;
}
Cephei::QL::Processes::CSquareRootProcess::CSquareRootProcess (QuantLib::Handle<QuantLib::SquareRootProcess> childNative)  : CStochasticProcess1D(CSquareRootProcess::typeid)
{
	_phSquareRootProcess = &childNative;
	_ppSquareRootProcess = &static_cast<boost::shared_ptr<QuantLib::SquareRootProcess>>(childNative.currentLink());
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppSquareRootProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CSquareRootProcess::CSquareRootProcess (QuantLib::SquareRootProcess childNative)  : CStochasticProcess1D(CSquareRootProcess::typeid)
{
#ifdef HANDLE
	_phSquareRootProcess = NULL;
#endif
	_ppSquareRootProcess = new boost::shared_ptr<QuantLib::SquareRootProcess> (new QuantLib::SquareRootProcess (childNative));
    _ppStochasticProcess1D = new boost::shared_ptr<QuantLib::StochasticProcess1D> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess1D> (*_ppSquareRootProcess));
}
#endif

Cephei::QL::Processes::CSquareRootProcess::~CSquareRootProcess ()
{
    if (_ppSquareRootProcess != NULL)
    {
	    delete _ppSquareRootProcess;
        _ppSquareRootProcess = NULL;
    }
}
Cephei::QL::Processes::CSquareRootProcess::!CSquareRootProcess ()
{
    if (_ppSquareRootProcess != NULL)
    {
	    delete _ppSquareRootProcess;
    }
}
QuantLib::SquareRootProcess& Cephei::QL::Processes::CSquareRootProcess::GetReference ()
{
    if (_ppSquareRootProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppSquareRootProcess;
}
boost::shared_ptr<QuantLib::SquareRootProcess>& Cephei::QL::Processes::CSquareRootProcess::GetShared ()
{
    if (_ppSquareRootProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppSquareRootProcess;
}
QuantLib::SquareRootProcess* Cephei::QL::Processes::CSquareRootProcess::GetPointer ()
{
    if (_ppSquareRootProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppSquareRootProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SquareRootProcess>& Cephei::QL::Processes::CSquareRootProcess::GetHandle ()
{
	if (_phSquareRootProcess == NULL)
	{
		_phSquareRootProcess = new Handle<QuantLib::SquareRootProcess> (*_ppSquareRootProcess);
	}
	return *_phSquareRootProcess;
}
#endif
bool Cephei::QL::Processes::CSquareRootProcess::HasNative () 
{
	return (_ppSquareRootProcess != NULL);
}

Double Cephei::QL::Processes::CSquareRootProcess::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSquareRootProcess)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CSquareRootProcess::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSquareRootProcess)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CSquareRootProcess::X0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSquareRootProcess)->x0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::ISquareRootProcess^ Cephei::QL::Processes::CSquareRootProcess_Factory::Create (Double b, Double a, Double sigma, Microsoft::FSharp::Core::FSharpOption<Double>^ x0)
{
    return REFNEW CSquareRootProcess ( b,  a,  sigma,  x0);
}
